{extend name='public/layer_base' /}
{block name='content'}
<style>
    .layui-form-label {
        width: 90px;
    }

    .layui-form-checkbox span {
        height: auto;
    }

    .extend-div {
        display: none;
        position: absolute;
        z-index: 2;
    }


    .more-setting .extend-div {
        width: 700px;
        margin-right: 0;
        margin-left: -630px;
    }

    .more-setting .more-from {
        margin-bottom: 20px;
    }

    .validateScene {
        min-width: 450px;
        width: 550px;
    }

    .layui-table td,
    .layui-table th {
        max-width: 50px;
        text-align: center;
    }
    .relation-input{
        display: none;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-row">

                <form class="layui-form" action="{:url('create')}" method="post">
                    <div class="layui-row">
                        <div class="col-md-12">
                            <div class="box">
                                <div class="box-header with-border">
                                    <h3 class="box-title">代码自动生成</h3>
                                </div>
                                <div class="layui-row">
                                    <div class="layui-col-md3">
                                        <div class="layui-form-item">
                                            <label for="table_name" class="layui-form-label">选择表</label>
                                            <div class="layui-input-inline">
                                                <select lay-verify="required" lay-search name="table_name"
                                                    id="table_name" lay-filter="table_name">
                                                    <option value="">请选择表</option>
                                                    {foreach name='table' id='item'}
                                                    <option value="{$item}">{$item}</option>
                                                    {/foreach}
                                                </select>
                                            </div>
                                            <button id="refreshTable" type="button" class="layui-btn" title="刷新表数据">
                                                <i class="fa fa-refresh"></i>
                                            </button>
                                        </div>
                                    </div>

                                    <div class="layui-col-md3">
                                        <div class="layui-form-item">
                                            <label for="cn_name" class="layui-form-label">中文名</label>
                                            <div class="layui-input-inline">
                                                <input name="cn_name" id="cn_name" class="layui-input"
                                                    placeholder="请填写中文名" title="表及功能的中文名字，例如用户表，就填写[用户]">
                                            </div>
                                        </div>
                                    </div>

                                    <div class="layui-col-md3">
                                        <div class="layui-form-item">
                                            <label for="module_name_suffix" class="layui-form-label">模块名称后缀</label>
                                            <div class="layui-input-inline">

                                                <input name="module_name_suffix" id="module_name_suffix"
                                                    class="layui-input" placeholder="模块名称后缀，可空"
                                                    title="默认为[管理]，例如前面的中文名叫[用户]，这里填写的是[管理]，那生成的菜单名称就叫[用户管理]"
                                                    value="管理">
                                            </div>
                                        </div>
                                    </div>

                                    <div class="layui-col-md3">
                                        <div class="layui-form-item">
                                            <label for="table_name" class="layui-form-label"><i
                                                    class="layui-icon fa-align-justify"></i></label>
                                            <div class="layui-input-block">
                                                <input maxlength="50" id="module_icon" name="module_icon"
                                                    value="fa-list" class="form-control " placeholder="请输入图标class"
                                                    title="模块图标，比如用户模块，可以选择[fa-user]作为图标，默认为[fa-list]">
                                            </div>
                                        </div>
                                    </div>
                                    <script>
                                        layui.use(['iconPickerFa'], function () {
                                            var iconPickerFa = layui.iconPickerFa;
                                            iconPickerFa.render({
                                                // 选择器，推荐使用input
                                                elem: '#module_icon',
                                                // 数据类型：fontClass/awesome，推荐使用fontClass
                                                url: "__ADMIN_PLUGINS__/font-awesome/less/variables.less",
                                                // 是否开启搜索：true/false
                                                search: true,
                                                // 是否开启分页
                                                page: true,
                                                // 每页显示数量，默认12
                                                limit: 12,
                                                // 点击回调
                                                click: function (data) {
                                                    console.log(data);
                                                },
                                                // 渲染成功后的回调
                                                success: function (d) {
                                                    console.log(d);
                                                }
                                            });
                                        });
                                    </script>

                                </div>
                                <div class="layui-row">
                                    <div class="layui-col-md3">
                                        <div class="layui-form-item">
                                            <label for="create_menu" class="layui-form-label">菜 单</label>
                                            <div class="layui-input-inline">
                                                <select lay-verify="required" lay-search name="create_menu"
                                                    id="create_menu">
                                                    <option value="-1">不生成</option>
                                                    <option value="0">顶级菜单</option>
                                                    {$menus|raw}
                                                </select>
                                            </div>
                                            <button id="refreshMenu" type="button" class="layui-btn" title="刷新表数据">
                                                <i class="fa fa-refresh"></i>
                                            </button>
                                        </div>
                                    </div>

                                    <div class="layui-col-md4">
                                        <div class="layui-form-item">
                                            <label for="create_menu_list" class="layui-form-label">菜 单</label>
                                            <div class="layui-input-block">
                                                <div id="create_menu_list"></div>
                                            </div>
                                            <script>
                                                var create_menu_list = xmSelect.render({
                                                    el: '#create_menu_list',
                                                    filterable: true,
                                                    name: 'create_menu_list',
                                                    data: [{
                                                            name: '列表',
                                                            value: 'index',
                                                            selected: true,
                                                            disabled: true
                                                        },
                                                        {
                                                            name: '添加',
                                                            value: 'add',
                                                            selected: true
                                                        },
                                                        {
                                                            name: '修改',
                                                            value: 'edit',
                                                            selected: true
                                                        },
                                                        {
                                                            name: '删除',
                                                            value: 'del',
                                                            selected: true
                                                        },
                                                        {
                                                            name: '启用',
                                                            value: 'enable',
                                                        },
                                                        {
                                                            name: '禁用',
                                                            value: 'disable',
                                                        },
                                                        {
                                                            name: '导出',
                                                            value: 'export',
                                                        },
                                                        {
                                                            name: '导入',
                                                            value: 'import',
                                                        },
                                                    ]
                                                })
                                            </script>
                                        </div>
                                    </div>


                                    <div class="layui-col-md1">
                                        <div class="layui-form-item">
                                            <div class="layui-input-block">
                                                <input checked type="checkbox" value="1" id="auto_class_name"
                                                    lay-skin="primary" name="auto_class_name" title="自动生成类名">
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="layui-row">
                                    <div class="layui-col-md3">
                                        <div class="layui-form-item">
                                            <label for="create_admin_controller" class="layui-form-label">Admin控制器</label>
                                            <div class="layui-input-inline">
                                                <select lay-verify="required" lay-search name="create_admin_controller"
                                                    id="create_admin_controller">
                                                    <option value="1">生成</option>
                                                    <option value="0">不生成</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                
                                    <div class="layui-col-md3">
                                        <div class="layui-form-item">
                                            <label for="admin_controller_name" class="layui-form-label">控制器名</label>
                                            <div class="layui-input-inline">
                                                <input name="admin_controller_name" id="admin_controller_name" class="layui-input"
                                                    placeholder="请填写控制器名" title="默认会根据表名自动生成">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="layui-col-md4">
                                        <label for="admin_controller_action_list" class="layui-form-label">控制器方法</label>
                                        <div class="layui-input-block">
                                            <div id="admin_controller_action_list"></div>
                                        </div>
                                        <script>
                                            var admin_controller_action_list = xmSelect.render({
                                                el: '#admin_controller_action_list',
                                                filterable: true,
                                                name: 'admin_controller_action_list',
                                                data: [{
                                                        name: '列表',
                                                        value: 'index',
                                                        selected: true
                                                    },
                                                    {
                                                        name: '添加',
                                                        value: 'add',
                                                        selected: true
                                                    },
                                                    {
                                                        name: '修改',
                                                        value: 'edit',
                                                        selected: true
                                                    },
                                                    {
                                                        name: '删除',
                                                        value: 'del',
                                                        selected: true
                                                    },
                                                    {
                                                        name: '启用',
                                                        value: 'enable',
                                                    },
                                                    {
                                                        name: '禁用',
                                                        value: 'disable',
                                                    },
                                                    {
                                                        name: '导出',
                                                        value: 'export',
                                                    }, {
                                                        name: '导入',
                                                        value: 'import',
                                                    },
                                                ]
                                            })
                                        </script>
                                    </div>
                                </div>
                                <div class="layui-row">
                                    <div class="layui-col-md3">
                                        <div class="layui-form-item">
                                            <label for="create_api_controller" class="layui-form-label">Api控制器</label>
                                            <div class="layui-input-inline">
                                                <select lay-verify="required" lay-search name="create_api_controller"
                                                    id="create_api_controller">
                                                    <option value="1">生成</option>
                                                    <option value="0">不生成</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                
                                    <div class="layui-col-md3">
                                        <div class="layui-form-item">
                                            <label for="api_controller_name" class="layui-form-label">控制器名</label>
                                            <div class="layui-input-inline">
                                                <input name="api_controller_name" id="api_controller_name" class="layui-input"
                                                    placeholder="请填写控制器名" title="默认会根据表名自动生成">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="layui-col-md4">
                                        <label for="api_controller_action_list" class="layui-form-label">控制器方法</label>
                                        <div class="layui-input-block">
                                            <div id="api_controller_action_list"></div>
                                        </div>
                                        <script>
                                            var api_controller_action_list = xmSelect.render({
                                                el: '#api_controller_action_list',
                                                filterable: true,
                                                name: 'api_controller_action_list',
                                                data: [{
                                                        name: '列表',
                                                        value: 'index',
                                                        selected: true
                                                    },
                                                    {
                                                        name: '添加',
                                                        value: 'add',
                                                        selected: true
                                                    },
                                                    {
                                                        name: '详情',
                                                        value: 'info',
                                                        selected: true
                                                    },
                                                    {
                                                        name: '修改',
                                                        value: 'edit',
                                                        selected: true
                                                    },
                                                    {
                                                        name: '删除',
                                                        value: 'del',
                                                        selected: true
                                                    },
                                                    {
                                                        name: '启用',
                                                        value: 'enable',
                                                    },
                                                    {
                                                        name: '禁用',
                                                        value: 'disable',
                                                    },
                                                ]
                                            })
                                        </script>
                                    </div>
                                </div>
                                                                
                                <div class="layui-row">
                                    <div class="layui-col-md3">
                                        <div class="layui-form-item">
                                            <label for="create_model" class="layui-form-label">模 型</label>
                                            <div class="layui-input-inline">
                                                <select lay-verify="required" lay-search name="create_model"
                                                    id="create_model">
                                                    <option value="1">生成</option>
                                                    <option value="0">不生成</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="layui-col-md3">
                                        <div class="layui-form-item">
                                            <label for="model_name" class="layui-form-label">模型名</label>
                                            <div class="layui-input-inline">
                                                <input name="model_name" id="model_name" class="layui-input"
                                                    placeholder="模型名" title="默认会根据表名自动生成">
                                            </div>
                                        </div>
                                    </div>

                                    <div class="layui-col-md1">
                                        <div class="layui-form-item">
                                            <div class="layui-input-block">
                                                <input checked type="checkbox" value="1" id="auto_timestamp"
                                                    lay-skin="primary" name="auto_timestamp" title="自动时间戳">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="layui-col-md1">
                                        <div class="layui-form-item">
                                            <div class="layui-input-block">
                                                <input checked type="checkbox" value="1" id="soft_delete"
                                                    lay-skin="primary" name="soft_delete" title="软删除">
                                            </div>
                                        </div>
                                    </div>

                                </div>
                                <div class="layui-row">
                                    <div class="layui-col-md3">
                                        <div class="layui-form-item">
                                            <label for="create_validate" class="layui-form-label">验证器</label>
                                            <div class="layui-input-inline">
                                                <select lay-verify="required" lay-search name="create_validate"
                                                    id="create_validate">
                                                    <option value="1">生成</option>
                                                    <option value="0">不生成</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="layui-col-md3">
                                        <div class="layui-form-item">
                                            <label for="validate_name" class="layui-form-label">验证器名</label>
                                            <div class="layui-input-inline">
                                                <input name="validate_name" id="validate_name" class="layui-input"
                                                    placeholder="验证器名" title="默认会根据表名自动生成">
                                            </div>
                                        </div>
                                    </div>


                                </div>

                                <div class="layui-row">
                                    <div class="layui-col-md3">
                                            <label for="create_view_index" class="layui-form-label">列表视图</label>
                                            <div class="layui-input-inline">
                                                <select lay-verify="required" lay-search name="create_view_index"
                                                    id="create_view_index">
                                                    <option value="1">生成</option>
                                                    <option value="0">不生成</option>
                                                </select>
                                            </div>
                                    </div>

                                    <div class="layui-col-md3">
                                        <div class="layui-form-item">
                                            <label for="create_view_index" class="layui-form-label">操作按钮形式</label>
                                            <div class="layui-input-inline">
                                                <select lay-verify="required" lay-search name="create_view_index"
                                                    id="create_view_index">
                                                    <option value="1">图标</option>
                                                    <option value="2">文字</option>
                                                    <option value="3">图标+文字</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="layui-col-md1">
                                        <div class="layui-form-item">
                                            <div class="layui-input-block">
                                                <input checked type="checkbox" value="1" id="list_delete"
                                                    lay-skin="primary" name="list_delete" title="列表删除">
                                            </div>
                                        </div>
                                    </div>

                                    <div class="layui-col-md1">
                                        <div class="layui-form-item">
                                            <div class="layui-input-block">
                                                <input checked type="checkbox" value="1" id="list_create"
                                                    lay-skin="primary" name="list_create" title="列表添加">
                                            </div>
                                        </div>
                                    </div>



                                    <div class="layui-col-md1">
                                        <div class="layui-form-item">
                                            <div class="layui-input-block">
                                                <input type="checkbox" value="1" id="list_export" lay-skin="primary"
                                                    name="list_export" title="列表导出">
                                            </div>
                                        </div>
                                    </div>

                                    <div class="layui-col-md1">
                                        <div class="layui-form-item">
                                            <div class="layui-input-block">
                                                <input type="checkbox" value="1" id="list_enable"
                                                    lay-filter="list_enable" lay-skin="primary" name="list_enable"
                                                    title="列表启用/禁用">
                                            </div>
                                        </div>
                                    </div>

                                </div>

                                <div class="layui-row">
                                    <div class="layui-col-md3">
                                        <div class="layui-form-item">
                                            <label for="create_view_add" class="layui-form-label">表单视图</label>
                                            <div class="layui-input-inline">
                                                <select lay-verify="required" lay-search name="create_view_add"
                                                    id="create_view_add">
                                                    <option value="1">生成</option>
                                                    <option value="0">不生成</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                            </div>
                        </div>
                    </div>
                    <table class="layui-table " style="text-align: center;">
                        <thead>
                            <tr>
                                <th>操作</th>
                                <th>
                                    <span title="字段名为小写下划线形式">字段</span> | <span
                                        title="字段类型，例如varchar(255),int(10),decimal(12,2)">类型</span>
                                </th>
                                <th>

                                    <span title="中文显示名称">
                                        名称
                                    </span>
                                    |
                                    <span title="表单内字段默认值，默认为数据库默认值，可自行修改">
                                        默认值
                                    </span>

                                </th>
                                <th>
                                    <span title="是否在后台列表展示">
                                        列表
                                    </span>
                                    |
                                    <span title="是否参与列表排序">
                                        排序
                                    </span>
                                    |
                                    <span title="是否参与列表筛选">
                                        筛选
                                    </span>
                                </th>
                                <th>
                                    <span title="后台添加/修改页表单字段的输入类型">
                                        表单类型
                                    </span>
                                    |
                                    <span title="后台(api)添加/修改时的表单验证规则">
                                        验证规则
                                    </span>

                                </th>
                                <th>
                                    <span title="该字段是否使用修改器，可选择的有开关，日期，日期时间等">
                                        修改器
                                    </span>
                                    |
                                    <span title="参与验证的场景，包括[admin_add,admin_edit,api_add,api_edit]">
                                        验证场景
                                    </span>
                                </th>
                                <th>
                                    <span data-toggle="tooltip"
                                    title="自定义筛选/select数据，当前字段为 列表选择 的时候生效。格式为：value||name，多个选项换行即可。例如[性别]字段，表单是[select]形式的，数据内容为：0||保密  1||男  2||女。一定要注意value和name的分割竖线为英文竖线，非中文竖线，不同的选项一定要换行">
                                    自定义列表
                                </span>
                                </th>
                                <th title="选择字段是否为关联字段以及关联的类型">
                                        关联
                                </th>
                            </tr>

                        </thead>
                        <tbody id="dataBody">

                        </tbody>
                    </table>
                    <div class="box-footer">
                        <button class="layui-btn layui-bg-blue" type="button" id="add">添加一行</button>
                        <button class="layui-btn  pull-right" type="button" lay-filter="submit" id="submit"
                            lay-submit="submit">生成代码</button>
                    </div>
                </form>

                {include file='generate/template' /}

            </div>
        </div>
    </div>

    </section>
    <script>
        $(function () {

            layui.use(['form', 'layer', 'table', 'element'], function () {
                var form = layui.form,
                    layer = layui.layer,
                    table = layui.table;
                //刷新数据库表

                $('#refreshTable').click(function () {
                    $.post('{:url("admin/generate/getTable")}', function (result) {
                        let html = '<option value=""></option>';
                        $.each(result.data, function (index, item) {
                            html += '<option value="' + item + '">' + item +
                                '</option>';
                        });
                        $('#table_name').html(html)
                        form.render('select');
                    });
                })

                //表单索引

                form.on('select(table_name)', function (data) {
                    getField(data.value);
                    form.render();
                });
                //列表启用/禁止关联菜单和控制器

                form.on('checkbox(list_enable)', function (data) {
                    if (data.elem.checked) {
                        controller_action_list.append([5]);
                        create_menu_list.append([5]);
                    } else {
                        controller_action_list.delete([5]);
                        create_menu_list.delete([5]);
                    }
                })

                var formIndex = 0;

                function addNewField(obj, type) {

                    formIndex++;
                    let template = $("#data-template").html().replace(/FORM_INDEX/g, '[' + formIndex +
                        ']').replace(/INDEX_ID/g, formIndex);
                    if (obj == null) {
                        $("#dataBody").append(template);
                    } else {
                        if (type === 1) {
                            $(obj).parent().parent().before(template);
                        } else {
                            $(obj).parent().parent().after(template);
                        }
                    }
                    //刷新 dataBody DOM后的操作
                    dataBodyRefreshed();
                }
                //默认添加一行空的字段
                addNewField(null, 1);

                $('#add').click(function (data) {
                    addNewField(null, 2)

                })


                //删除当前字段
                function delThisField(obj) {
                    layer.confirm('您确认删除本行吗？', {
                        title: '删除确认',
                        closeBtn: 1,
                        icon: 3
                    }, function () {
                        $(obj).parent().parent().remove();
                        layer.closeAll();
                    });
                }

                //获取表信息
                function getField(name) {
                    let loadT = layer.msg('正在加载，请稍候…', {
                        icon: 16,
                        time: 0,
                        shade: [0.3, "#000"],
                        scrollbar: false,
                    });
                    $.post('getField', {
                        name: name
                    }, function (result, status) {
                        layer.close(loadT);

                        if (status == 'success') {
                            let $dataBody = $("#dataBody");

                            if (adminDebug) {
                                console.time('表信息与相关DOM操作耗时');
                            }
                            let controller = result.data.controller;
                            let model = result.data.model;
                            let validate = result.data.validate;
                            let field = result.data.field;
                            if (adminDebug) {
                                console.log('字段数量：' + field.length);
                            }
                            let table = result.data.table;
                            //中文名
                            $('#cn_name').val(table.cn_name);
                            //如果是自动生成类名
                            if ($('#auto_class_name').is(':checked')) {
                                $("#admin_controller_name").val(controller.name);
                                $("#api_controller_name").val(controller.name);
                                $("#model_name").val(model.name);
                                $("#validate_name").val(validate.name);
                            }
                            $dataBody.html('');
                            //字段名
                            let field_name =
                                "#data-template input[name='field_nameFORM_INDEX']";
                            //字段类型
                            let field_type =
                                "#data-template input[name='field_typeFORM_INDEX']";
                            //是否为列表字段
                            let field_list = "#data-template [name='is_listFORM_INDEX']";
                            //是否为表单字段
                            let field_form = "#data-template [name='is_formFORM_INDEX']";
                            //中文名称
                            let form_name = "#data-template [name='form_nameFORM_INDEX']";
                            //表单类型
                            let form_type = "#data-template [name='form_typeFORM_INDEX']";
                            //表单验证
                            let form_validate =
                                "#data-template [name='form_validateFORM_INDEX']";
                            //字段默认值
                            let field_form_default =
                                "#data-template [name='field_defaultFORM_INDEX']";
                            //获取器/修改器
                            let getter_setter =
                                "#data-template [name='getter_setterFORM_INDEX']";
                            //列表排序
                            let list_sort = "#data-template [name='list_sortFORM_INDEX']";
                            let xmIndex = formIndex;

                            let listHtml = '';

                            let relation_type =
                                "#data-template [name='relation_typeFORM_INDEX']";
                            let relation_show =
                                "#data-template [name='relation_showFORM_INDEX']";
                            let relation_table =
                                "#data-template [name='relation_tableFORM_INDEX']";
                            $.each(field, function (index, item) {
                                //字段名
                                $(field_name).attr('value', item.name);
                                //字段类型
                                $(field_type).attr('value', item.field_type);
                                //中文名称
                                $(form_name).attr('value', item.cn_name);
                                //默认值
                                $(field_form_default).attr('value', item.default);
                                //表单类型
                                $(form_type).val(item.form_type);

                                //是否为列表字段
                                if (item.is_list === 1) {
                                    $(field_list).attr('checked', true);
                                } else {
                                    $(field_list).attr('checked', false);
                                }

                                //是否为表单字段
                                if (item.is_form === 1) {
                                    $(field_form).attr('checked', true);
                                } else {
                                    $(field_form).attr('checked', false);
                                }

                                //表单类型
                                $(form_type).find("option[value='" + item
                                    .form_type + "']").attr("selected", true);
                                //获取器/修改器
                                if (item.getter_setter !== false) {
                                    $(getter_setter).find("option[value='" + item
                                        .getter_setter + "']").attr("selected",
                                        true);
                                }

                                $(relation_type).find("option[value='" + item
                                    .relation_type + "']").attr("selected", true);
                                if (item.relation_type === 2) {
                                    // 这里处理显示
                                    $(relation_show).attr('value', item.relation_show);
                                    $(relation_show).show();

                                } else if (item.relation_type === 4) {
                                    $(relation_table).show();
                                    $(relation_table).attr('value', item
                                        .relation_table);
                                }

                                //优化生成DOM慢的问题
                                formIndex++;
                                listHtml += $("#data-template").html().replace(
                                        /FORM_INDEX/g, '[' + formIndex + ']')
                                    .replace(/INDEX_ID/g, formIndex);

                                if (getter_setter !== false) {
                                    $(getter_setter).find("option[value='" + item
                                        .getter_setter + "']").attr("selected",
                                        false);
                                }
                                $(form_type).find("option[value='" + item
                                    .form_type + "']").attr("selected", false);
                                $(field_name).attr('value', '');
                                $(field_type).attr('value', '');
                                $(form_validate).empty();
                                $(relation_show).attr('value', '');
                                $(relation_table).attr('value', '');
                                $(relation_show).hide();
                                $(relation_table).hide();
                            });
                            $dataBody.append(listHtml);
                            $.each(field, function (index, item) {
                                xmIndex++;
                                xmSelect.render({
                                    el: '#form_validate' + xmIndex,
                                    filterable: true,
                                    name: 'form_validate' + '[' + xmIndex +
                                        ']' + '',
                                    data: item.validate_html
                                })
                            });
                            dataBodyRefreshed();

                            if (adminDebug) {
                                console.timeEnd('表信息与相关DOM操作耗时');
                            }

                        } else {

                            layer.msg('服务器错误', {
                                icon: 2,
                                scrollbar: false,
                            });
                        }

                    });
                }
                //表单元素类型改变

                form.on('select(selectField)', function (data) {
                    let form_type = data.value;
                    let index = $(data.elem).data('index');
                    $.post('getValidateSelect', {
                        form_type: form_type
                    }, function (result) {
                        if (200 === result.code) {
                            xmSelect.render({
                                el: '#form_validate' + index,
                                filterable: true,
                                name: 'form_validate' + '[' + index + ']' +
                                    '',
                                data: result.data
                            })
                            form.render('select');
                        } else {
                            layer.msg(result.msg);
                        }
                    });

                })

                //刷新菜单
                $('#refreshMenu').click(function () {
                    $.post('{:url("admin/generate/getMenu")}', function (result) {
                        let html =
                            '<option value="-1">不生成</option><option value="0">顶级菜单</option>';
                        html += result.data;
                        $('#create_menu').html(html);
                        form.render('select');
                    });
                })

                // 选择关联类型

                form.on('select(relation)', function (data) {

                    let $obj = $(data.elem);
                    let relation_type = parseInt(data.value);
                    let relation_show = $obj.data('relationShow');
                    let relation_table = $obj.data('relationTable');
                    let field = data.value;
                    switch (relation_type) {
                        case 1:
                        case 2:
                            $('input[name="' + relation_table + '"]').hide();
                            $('input[name="' + relation_show + '"]').show();
                            $.post('{:url("admin/generate/getRelationShowField")}', {
                                field: field
                            }, function (result) {
                                $('input[name="' + relation_show + '"]').val(result.data
                                    .name);
                            });
                            $('input[name="' + relation_table + '"]').attr('readonly',
                                'readonly');
                            $('input[name="' + relation_show + '"]').removeAttr('readonly');
                            break;
                        case 3:
                        case 4:
                            $.post('{:url("admin/generate/getRelationTable")}', {
                                field: field
                            }, function (result) {
                                $('input[name="' + relation_table + '"]').val(result
                                    .data.name);
                            });
                            $('input[name="' + relation_show + '"]').hide();
                            $('input[name="' + relation_table + '"]').show();

                            $('input[name="' + relation_show + '"]').attr('readonly',
                                'readonly');
                            $('input[name="' + relation_table + '"]').removeAttr('readonly');
                            break;
                        default:
                            $('input[name="' + relation_table + '"]').hide();
                            $('input[name="' + relation_show + '"]').hide();
                            break;
                    }

                })

                //列表刷新后的操作
                function dataBodyRefreshed() {
                    form.render();
                }


            });
        });
    </script>

    {/block}